package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
@Transactional
public class WmMaterialServiceImp extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //判断文件是否为空
        if (multipartFile == null || multipartFile.getSize() == 0){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断用户是否为空
        WmUser user = WmThreadLocalUtil.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //注入minio依赖，配置minio线上nacos文件
        //准备文件名，使用UUID
        String filename = UUID.randomUUID().toString();
        //获取文件后缀名
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        //注入minio工具,上传图片
        String path = fileStorageService.uploadImgFile("", filename + suffix, multipartFile.getInputStream());
        //通过WmMaterial保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setUserId(user.getId());
        wmMaterial.setUrl(path);
        wmMaterial.setCreatedTime(new Date());
        //保存
        save(wmMaterial);
        //返回参数
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //检验参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //执行设置默认值方法
        dto.checkParam();
        //查询用户
        WmUser user = WmThreadLocalUtil.getUser();
        if (user ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //设置分页参数
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        //设置查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();

        if (dto.getIsCollection() != 0 && dto.getIsCollection() == 1){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //只查询当前用户的
        queryWrapper.eq(WmMaterial::getUserId,user.getId());
        //分页查询
        page(page,queryWrapper);
        //返回数据
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setCurrentPage(dto.getPage());
        responseResult.setSize(dto.getSize());
        responseResult.setTotal((int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}
